package pkg

import (
	"fmt"
	"github.com/emicklei/go-restful"
	"strconv"
	"strings"
)

type ResponseResult struct {
	Code   int64       `json:"code"`
	Result interface{} `json:"result"`
}

const (
	HealthCheckError       string = "400-00001"
	DictionaryError        string = "400-00002"
	AuthorityError         string = "400-00003"
	CreateTrancactionError string = "400-00004"
	DeleteTransactionError string = "400-00005"
	UpdateTransactionError string = "400-00006"
	ListTransactionError   string = "400-00007"
)

func WriteResponse(resp *restful.Response, code string, result interface{}) {
	httpCode, res := NewResponseResult(code, result)

	_ = resp.WriteHeaderAndEntity(httpCode, res)
}

func NewResponseResult(code string, result interface{}) (int, ResponseResult) {
	codes := strings.Split(code, "-")
	httpCode, _ := strconv.Atoi(codes[0])
	statusCode, _ := strconv.ParseInt(codes[1], 10, 64)

	return httpCode, ResponseResult{
		Code:   statusCode,
		Result: fmt.Sprint(result),
	}
}
